package com.lczc.soa.controller;

import com.lczc.soa.domain.*;
import com.lczc.soa.domain.Door;
import com.lczc.soa.domain.SearchDoorResponse;

import com.lczc.soa.service.AreaService;
import com.lczc.soa.service.DoorService;
import com.lczc.soa.service.StreetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@RequestMapping("/Door")
public class DoorController {

    @Autowired
    private DoorService doorService;

    @Autowired
    private StreetService streetService;

    @Autowired
    private AreaService areaService;

    @RequestMapping("/toadd")
    public String addDoorPage(Model model){
        model.addAttribute("door",new Door());
        return "door_add";
    }

    @RequestMapping("/add")
    public String addDoor(Door door){
        int updated = doorService.saveDoor(door);
        return "redirect:/Door/list";
    }

    @RequestMapping("/list")
    public String DoorList(){
        return "door_list";
    }

    @GetMapping("/api/search")
    @ResponseBody
    public SearchDoorResponse searchDoorResponse(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize){
        return doorService.findAllDoor(pageNum,pageSize);
    }

    @GetMapping("/api/searchLike")
    @ResponseBody
    public SearchDoorResponse searchDoorResponseLike(@RequestParam("keywords") String keywords,
                                                     @RequestParam("pageNum") Integer pageNum,
                                                     @RequestParam("pageSize") Integer pageSize){
        return doorService.findDoorLikeDoorNumber(keywords,pageNum,pageSize);
    }

    @RequestMapping("/edit")
    public String EditDoor(@RequestParam("id") String id, Model model){
        Door door = doorService.findDoorByPrimaryKey(id);
        model.addAttribute("door",door);
        return "door_add";
    }

    @DeleteMapping("/delete")
    @ResponseBody
    public Boolean deleteDoor(@RequestParam("id") String id){
        int deleted = doorService.deleteDoor(id);
        return deleted >0;
//        if (deleted > 0){
//            return "/Door/list";
//        }else {
//            return "sorry";
//        }
    }

    @RequestMapping("/findAreaByStreet")
    @ResponseBody
    public Area findAreaByStreet(@RequestParam("id") String id){
        return areaService.findAreaByStreet(id);
    }

    @RequestMapping("/findStreetByDoor")
    @ResponseBody
    public Street findStreetByDoor(@RequestParam("id") String id){
        return streetService.findStreetByDoor(id);
    }

    @RequestMapping("/findArea")
    @ResponseBody
    public List<Area> findArea(){
        return areaService.listArea();
    }

    @RequestMapping("/findStreetByArea")
    @ResponseBody
    public List<Street> findStreetByArea(@RequestParam("id") String id){
        return streetService.findStreetByArea(id);
    }

}
